package com;

import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author wdl
 */
@WebServlet(name = "ForgetPasswordServlet")
public class ForgetPasswordServlet extends HttpServlet {
    private static final String QUERY_OPERATION="query";
    private static final String UPDATE_OPERATION="update";
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //charset=utf-8使得可以显示中文
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        //获取操作类型
        String operation = request.getParameter("operation");
        //从客户端获取数据
        String account = request.getParameter("account");

        //打印获得的值
        System.out.println(operation + account);

        //获得数据库操作对象
        CarDB cardb = new CarDB();
        //new一个JSONObject存放数据
        JSONObject object = new JSONObject();
        //查询电话的操作
        if (operation.equals(QUERY_OPERATION)) {
            //查找账号信息
            String tel = cardb.queryTelByAccount(account);
            //如果未查到电话，则说明账号为空返回0，不为空则返回1
            if (tel == null) {
                object.put("result", 0);
            } else {
                object.put("result", 1);
            }
            //放入电话
            object.put("tel", tel);
        } else if (operation.equals(UPDATE_OPERATION)) {
            String newPassword = request.getParameter("newPassword");
            int result = cardb.updatePassword(account, newPassword);
            object.put("result", result);
        }
        //输出信息
        out.print(object);

        out.flush();
        out.close();
    }
}
